#pragma once
#include <sstream>
#include "GSE_Net.h"
#include <Windows.h>
using namespace GSE;

class CClient
{
public:
	CClient(void){m_lpConnector = 0;}
	~CClient(void){}

	void Init()
	{
		m_bShutdown = false;
		m_lpConnector = INet::InstancePtr()->CreateConnector();
	}

	void SetCallback(ISessionCallback * lpCB)
	{
		m_lpConnector->SetSessionCallback(lpCB);
	}

	ISession * GetSession()
	{
		return m_lpConnector->GetSession();
	}

	void Connect(const char* strIP,const char * strPort)
	{
		m_lpConnector->Connect(strIP,strPort);
	}
	void Run()
	{
		while ( !m_bShutdown )
		{
			m_lpConnector->Run();
			Sleep(1);
		}		
	}
	void Uninit()
	{
		m_lpConnector->GetSession()->Close();
		INet::InstancePtr()->DestroyConnector(m_lpConnector);
	}

	void Shutdown()
	{
		m_bShutdown = true;
	}
private:
	IConnector * m_lpConnector;
	bool	m_bShutdown;

};


class CClientCallback_Test1 : public ISessionCallback
{
public:
	CClientCallback_Test1(CClient * lp,int i):lpClient(lp),m_lpSession(lpClient->GetSession()),m_I(i) {}
	virtual void OnConnected()
	{
		std::stringstream bb ;
		bb << "Hello,I am the : " << m_I;
		for ( int i = 0 ; i < 10; i ++ )
		{
			m_lpSession->Send(bb.str().c_str(),bb.str().size());
		}
	}
	virtual void OnReceive(const char * lpBuffer, uint32 uLen)
	{
		BOOST_CHECK(0);
	}
	virtual void OnError(int errCode,const char * errMsg)
	{
		LOG(LOG_GSE_CRITICAL) << errMsg;
		BOOST_CHECK(0);
	}
	virtual void OnDisconnected()
	{
		BOOST_CHECK(0);
	}
protected:
	CClient * lpClient;
	ISession * m_lpSession;
	int m_I;
};



class CClientCallback_Test2 : public ISessionCallback
{
public:
	CClientCallback_Test2(CClient * lp,int i):lpClient(lp),m_lpSession(lpClient->GetSession()),m_I(i) {}
	virtual void OnConnected()
	{
		char b[1024 * 30];
		memset(b,'a',1024 * 30);
		m_lpSession->Send(b,1024 * 30);
	}
	virtual void OnReceive(const char * lpBuffer, uint32 uLen)
	{
		BOOST_CHECK(0);
	}
	virtual void OnError(int errCode,const char * errMsg)
	{
		LOG(LOG_GSE_CRITICAL) << errMsg;
		BOOST_CHECK(0);
	}
	virtual void OnDisconnected()
	{
		BOOST_CHECK(0);
	}
protected:
	CClient * lpClient;
	ISession * m_lpSession;
	int m_I;
};

class CClientCallback_Test3 : public ISessionCallback
{
public:
	CClientCallback_Test3(CClient * lp,int i):lpClient(lp),m_lpSession(lpClient->GetSession()),m_I(i) {}
	virtual void OnConnected()
	{
		char b[1024 * 8];
		memset(b,'b',1024 * 8);
		m_lpSession->Send(b,1024 * 8);
	}
	virtual void OnReceive(const char * lpBuffer, uint32 uLen)
	{
		BOOST_CHECK(0);
	}
	virtual void OnError(int errCode,const char * errMsg)
	{
		LOG(LOG_GSE_CRITICAL) << errMsg;
		BOOST_CHECK(0);
	}
	virtual void OnDisconnected()
	{
		BOOST_CHECK(0);
	}
protected:
	CClient * lpClient;
	ISession * m_lpSession;
	int m_I;
};


class CClientCallback_Test4 : public ISessionCallback
{
public:
	CClientCallback_Test4(CClient * lp,int i):lpClient(lp),m_lpSession(lpClient->GetSession()),m_I(i) {}
	virtual void OnConnected()
	{
		char b[1024 * 128 - 8];
		memset(b,'c',1024 * 128 - 8);
		m_lpSession->Send(b,1024 * 128 - 8);
	}
	virtual void OnReceive(const char * lpBuffer, uint32 uLen)
	{
		BOOST_CHECK(0);
	}
	virtual void OnError(int errCode,const char * errMsg)
	{
		LOG(LOG_GSE_CRITICAL) << errMsg;
		BOOST_CHECK(0);
	}
	virtual void OnDisconnected()
	{
		BOOST_CHECK(0);
	}
protected:
	CClient * lpClient;
	ISession * m_lpSession;
	int m_I;
};


class CClientCallback_Test5 : public ISessionCallback
{
public:
	CClientCallback_Test5(CClient * lp,int i):lpClient(lp),m_lpSession(lpClient->GetSession()),m_I(i) {}
	virtual void OnConnected()
	{
		char b[1024 * 130];
		memset(b,'d',1024 * 130);
		m_lpSession->Send(b,1024 * 130);
	}
	virtual void OnReceive(const char * lpBuffer, uint32 uLen)
	{
		BOOST_CHECK(0);
	}
	virtual void OnError(int errCode,const char * errMsg)
	{
		LOG(LOG_GSE_CRITICAL) << errMsg;
		BOOST_CHECK(0);
	}
	virtual void OnDisconnected()
	{
		BOOST_CHECK(0);
	}
protected:
	CClient * lpClient;
	ISession * m_lpSession;
	int m_I;
};


class CClient_1
{
public:
	CClient_1(void){}
	~CClient_1(void){}

	void Init()
	{
		m_bShutdown = false;
		m_lpConnector = INet::InstancePtr()->CreateConnector();
	}

	void SetCallback(ISessionCallback * lpCB)
	{
		m_lpConnector->SetSessionCallback(lpCB);
	}

	ISession * GetSession()
	{
		return m_lpConnector->GetSession();
	}

	void Connect(const char* strIP,const char * strPort)
	{
		m_lpConnector->Connect(strIP,strPort);
	}
	void Run()
	{
		while ( !m_bShutdown )
		{
			m_lpConnector->Run();
			Sleep(1);
		}
	}
	void Uninit()
	{
		INet::InstancePtr()->DestroyConnector(m_lpConnector);
	}

	void Shutdown()
	{
		m_bShutdown = true;
	}
private:
	IConnector * m_lpConnector;
	bool	m_bShutdown;

};



class CClientCallback_Test6 : public ISessionCallback
{
public:
	CClientCallback_Test6(CClient_1 * lp,int i):lpClient(lp),m_lpSession(lpClient->GetSession()),m_I(i) {}
	virtual void OnConnected()
	{
		static const int maxA = 1024*30;
		char b[maxA];
		memset(b,'a',maxA);
		m_lpSession->Send(b,maxA);
	}
	virtual void OnReceive(const char * lpBuffer, uint32 uLen)
	{
		BOOST_CHECK(0);
	}
	virtual void OnError(int errCode,const char * errMsg)
	{
		LOG(LOG_GSE_CRITICAL) << errMsg;
		BOOST_CHECK(0);
	}
	virtual void OnDisconnected()
	{
		BOOST_CHECK(0);
	}
protected:
	CClient_1 * lpClient;
	ISession * m_lpSession;
	int m_I;
};